********************************************************************************
**	PURPOSE: Average and hetergeneous treatment effects of CBL on likelihood of being FICO scored and FICO score, create T3
**							
**	INPUTS: Long with outcomes.dta
**	
**	OUTPUTS: Take up and main effects.xls 
**				
**	CREATED/EDITED BY Kayla Wilding, Leah Kim, Hasan Ahamed
**
**	DATE CREATED: 6/22/2017
**
**	DATE LAST EDITED:	2/23/2023
********************************************************************************

/*******************************************************************************
0. Setup 
********************************************************************************/
clear all

*A. Filename and table title
local filename "Take up and main effects"
local tabletitle "Table 3. CBL Take-up and Primary Average Treatment Effects"

*B. Define the covariate regression spec options and the outcome variables of interest
local loanspec1 1.post 1.enc#1.post 
local loanspec2 1.pt1 1.pt2 1.pt3 1.enc#1.pt1 1.enc#1.pt2 1.enc#1.pt3

local outcomes op18 open_sd30d scoredf ficoscore08 
local decs 3

/*******************************************************************************
1. Use Long dataset
********************************************************************************/

use "$adta/Long with outcomes.dta", clear

/*******************************************************************************
2. Edit variable labels for output
********************************************************************************/

label variable enc "CBL Group"
label variable scoredf "1 = Has FICO Score"
label variable ficoscore08 "FICO Score"

local colall "All"
local colrest "Had score at baseline"
label var post "Post"

/*******************************************************************************
3. Run regressions, tests, and output for Table 3a and 3b
********************************************************************************/

local loopcount = 1
foreach var in `outcomes' {
	*Note:
	*(A). Define the sample restrictions based on the outcome variables. If the outcome variables is "scored on fico (scoredf), then we want the full 1531 sample, and if its ficoscore, then we want to restrict to those scored at baseline (flag_randomized_scored_base). 
	*(B). Calculate the mean of the dependent variable at each endline for the control group. This is to report at the bottom in extra statistics.	
		cap drop __*
	        if "`var'" == "op18" | "`var'" == "open_sd30d" { 
                   reg `var' enc if flag_randomized == 1 & index == 1, robust
                }
		else {
		areg `var' `loanspec1' if `=cond("`var'" == "ficoscore08", "flag_randomized_scored_base", "flag_randomized")' == 1, absorb(surveyid) vce(cluster surveyid) 
		}

		local obs = e(N)
		
		if "`var'" == "op18" | "`var'" == "open_sd30d" { 
			local inds = e(N) // number of observations
		}
		else{
			local inds = e(df_a) + 1 //df for absorb effects + 1 is our total number of indivisual
		}
		sum `var' if e(sample) == 1 & index == 1 & flag_extragroup == 1 // recording control group mean in baseline
		local mu_lhs = string(r(mean), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f") 
		local sd_lhs = string(r(sd), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f")
		sum `var' if e(sample) == 1 & flag_extragroup == 1 // recording control group mean
                local mu_lhs_cont = string(r(mean), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f") 
                local sd_lhs_cont = string(r(sd), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f")
		
		#delimit ;
		outreg2 using "$outputtables/`filename'.xls", `=cond(`loopcount' == 1,"replace","append")' drop(1.post 1.pt1 1.pt2 1.pt3 ) 
		  sortvar(enc 1.enc#1.post 1.enc#1.pt1 1.enc#1.pt2 1.enc#1.pt3) label nocons dec(`decs') nor2 noobs 
		  addtext(Obs, "`obs'", Individuals, "`inds'", 
		  Mean Dependent Variable in Extra Step Group, `mu_lhs_cont', 
		  SD Dep Var in Extra Step Group, `sd_lhs_cont', 
		  Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs',
		  SD Dep var in extra step group at baseline, `sd_lhs') 
		  title("`tabletitle'") slow(8000) nonote noaster excel;
			sleep 2000;
		#delimit cr;
		
		if "`var'" != "op18" & "`var'" != "open_sd30d" {
		areg `var' `loanspec2' if `=cond("`var'" == "ficoscore08", "flag_randomized_scored_base", "flag_randomized")' == 1, absorb(surveyid) vce(cluster surveyid) 
		local obs = e(N)
		local inds = e(df_a) + 1
		sum `var' if e(sample) == 1 & index == 1 & flag_extragroup == 1
		local mu_lhs = string(r(mean), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f") 
		local sd_lhs = string(r(sd), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f")
		sum `var' if e(sample) == 1 & flag_extragroup == 1
                local mu_lhs_cont = string(r(mean), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f") 
                local sd_lhs_cont = string(r(sd), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f")
		
		#delimit ;
		outreg2 using "$outputtables/`filename'.xls", append drop(1.post 1.pt1 1.pt2 1.pt3) 
		  sortvar(enc 1.enc#1.post 1.enc#1.pt1 1.enc#1.pt2 1.enc#1.pt3) label nocons dec(`decs') nor2 noobs 
		  addtext(Obs, "`obs'", Individuals, "`inds'", 
		  Mean Dependent Variable in Extra Step Group, `mu_lhs_cont', 
		  SD Dep Var in Extra Step Group, `sd_lhs_cont', 
		  Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs',
		  SD Dep var in extra step group at baseline, `sd_lhs') 
		  title("`tabletitle'") slow(8000) nonote noaster excel;
			sleep 2000;
		#delimit cr;
		}
		
		local ++loopcount
}

/*******************************************************************************
*. Footnote about CATE and functional form specification
********************************************************************************/
version 11
gen encpost = enc*post //interactions not allowed
dprobit scoredf post encpost if flag_randomized == 1, vce(cluster surveyid) // Related to Note 40 of the paper 

version 17 



**EOF**

